//
//  QDLeftMenuController.m
//  simulateQuickDoctor
//
//  Created by 梁家伟 on 17/3/22.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDLeftMenuController.h"
#import "QDLeftMenuCell.h"
#import "QDSliderController.h"
#import "QDTabBarController.h"
#import "QDBaseController.h"
#import <UIImageView+WebCache.h>



typedef enum:NSUInteger{
    QDLeftMenuCellTypeDoctorApply = 0,
    QDLeftMenuCellTypeSickCaseManger,
    QDLeftMenuCellTypeDoctorAttention,
    QDLeftMenuCellTypeSetting,
    QDLeftMenuCellTypeLogout
}QDLeftMenuCellType;



@interface QDLeftMenuController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,weak)UITableView* menuList ;
@property(nonatomic,assign)BOOL isLogin;
@property(nonatomic,weak)UIImageView*  iconView;
@property(nonatomic,weak)UIView* headerView;
@property(nonatomic,weak)UILabel* phoneLabel;
@property(nonatomic,weak)UILabel* nameLabel;
@end


@implementation QDLeftMenuController{
    NSArray* menuData;
}

static NSUInteger baseTag = 666;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadData];

}


/**
 设置左侧菜单 用户信息
 */
- (void)setHeaderView{
    
    UIView* headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.left.right.equalTo(self.view);
        make.height.mas_offset(100);
    }];
    
    UIImageView* iconView = [[UIImageView alloc]init];
    _headerView = headerView;
    _iconView = iconView;
    
    [headerView addSubview:iconView];
    
    
    
    
    [self setLoginView];
    

}


- (void)clipIcon{
    
    //加载图片
    UIImage *image = [UIImage imageNamed:@"doctor_defaultphoto_male.png"];
    
    //获取图片尺寸
    CGSize size = image.size;
    
    //开启位图上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    //创建圆形路径
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    
    //设置为裁剪区域
    [path addClip];
    
    //绘制图片
    [image drawAtPoint:CGPointZero];
    
    //获取裁剪后的图片
    _iconView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭上下文
    UIGraphicsEndImageContext();
    
}


- (void)setLoginView{
    
    QDUserInfo * userinfo = [QDUserInfo defaultUserInfo];
    
    if(userinfo.model !=  nil){
        _isLogin = YES;
    }
    
    
    if(_isLogin){
        
        _menuList.hidden = NO;
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headerView).offset(30);
            make.centerY.equalTo(_headerView);
            make.size.mas_equalTo(CGSizeMake(64, 64));
        }];
        
        NSLog(@"%@",userinfo.model.head_photo);
        
        [_iconView sd_setImageWithURL:[NSURL URLWithString:userinfo.model.head_photo] placeholderImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
        
        [self clipIcon];
        
        UILabel* nameLabel = [UILabel cz_labelWithText:userinfo.model.true_name fontSize:17 color:[UIColor whiteColor]];
        _nameLabel = nameLabel;
        [_headerView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_iconView.mas_right).offset(15);
            make.centerY.equalTo(_headerView).offset(-15);
        }];
        
        
        
        UILabel* phoneLabel = [UILabel cz_labelWithText:userinfo.model.mobile_number fontSize:17 color:[UIColor whiteColor]];
        [_headerView addSubview: phoneLabel];
        _phoneLabel = phoneLabel;
        
        [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_iconView.mas_right).offset(15);
            make.centerY.equalTo(_headerView).offset(15);
        }];
        
        
        _iconView.userInteractionEnabled =YES;
        
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpToUserInfo:)];
        
        [_iconView addGestureRecognizer:tap];
    }else{
        
        _menuList.hidden = YES;
        
        //注册按钮
        UIButton* registerBtn = [UIButton cz_textButton:@"注册" fontSize:17 normalColor:[UIColor whiteColor] selectedColor:[UIColor whiteColor]];
        
        [_headerView addSubview:registerBtn];
        
        [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headerView).offset(30);
            make.centerY.equalTo(_headerView).offset(25);
        }];

        
        
        //头像
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(registerBtn.mas_right).offset(30);
            make.centerY.equalTo(_headerView);
            make.size.mas_equalTo(CGSizeMake(64, 64));
        }];
        
        _iconView.image = [UIImage imageNamed:@"doctor_defaultphoto_male"];
        
        [self clipIcon];
        
        //登录按钮
        UIButton* loginBtn = [UIButton cz_textButton:@"登录" fontSize:17 normalColor:[UIColor whiteColor] selectedColor:[UIColor whiteColor]];
        
        [_headerView addSubview:loginBtn];
        [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_iconView.mas_right).offset(30);
            make.centerY.equalTo(_headerView).offset(25);
        }];
        
        
        [registerBtn addTarget:self action:@selector(loginOrRegister:) forControlEvents:UIControlEventTouchUpInside];
        
        [loginBtn addTarget:self action:@selector(loginOrRegister:) forControlEvents:UIControlEventTouchUpInside];
        
        registerBtn.tag = 0 + baseTag;
        loginBtn.tag = 1 + baseTag;
        
        
    }
}


- (void)jumpToUserInfo:(UITapGestureRecognizer*)tap{
    
    
    QDSliderController* parentVC = (QDSliderController*)self.parentViewController;
    
    QDTabBarController* rightVC = (QDTabBarController*)parentVC.rightVC;
    
    [parentVC resetTransform];
    
    [rightVC pushControllerWithStoryName:@"GeRen" andIdentifier:@"GeRen" andPara:nil];
    
    
}


- (void)loginBack{
    
    
//    QDUserInfo * userinfo = [QDUserInfo defaultUserInfo];
//    
//    
//    [_iconView sd_setImageWithURL:[NSURL URLWithString:userinfo.model.head_photo] placeholderImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
//    
//    _phoneLabel.text = userinfo.model.mobile_number;
//    _nameLabel.text = userinfo.model.true_name;
    [_headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self setHeaderView];
}


/**
 跳转登录注册界面
 */
- (void)loginOrRegister:(UIButton*)btn{
 
    NSUInteger type = btn.tag - baseTag;
    
    QDSliderController* parentVC = (QDSliderController*)self.parentViewController;
    
    QDTabBarController* rightVC = (QDTabBarController*)parentVC.rightVC;
    
    [parentVC resetTransform];
    
    void(^loginBlock)() = ^() {
        [[QDUserInfo defaultUserInfo] logIn];
        [self loginBack];
        [parentVC resetTransform];
        [QDBannerTools pp_BannerSuccessTitle:@"登录成功" andTime:1.0];
    };
    
    [rightVC pushControllerWithStoryName:@"Login" andIdentifier:@"Login" andPara:@{@"type":@(type),@"loginBlock":loginBlock}];
    
}



- (void)setupUI{
    self.view.backgroundColor = PPColor;
    
    [self setHeaderView];
    
    UITableView* menuList = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    menuList.backgroundColor = [UIColor clearColor];
    menuList.separatorInset = UIEdgeInsetsMake(0, 0, 0, self.view.bounds.size.width);
    _menuList = menuList;
    if([QDUserInfo defaultUserInfo].model == nil){
        _menuList.hidden = YES;
    }
    [self.view addSubview:menuList];
    menuList.dataSource = self;
    menuList.delegate = self;
    [menuList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(_headerView.mas_bottom);
    }];
    
    menuList.tableFooterView = [UIView new];

    
    [menuList registerClass:[QDLeftMenuCell class] forCellReuseIdentifier:@"leftmenu"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return menuData.count;
}

- (QDLeftMenuCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QDLeftMenuCell* cell = [tableView dequeueReusableCellWithIdentifier:@"leftmenu" forIndexPath:indexPath];
    cell.name = menuData[indexPath.row][@"name"];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    QDSliderController* parentVC = (QDSliderController*)self.parentViewController;
    [parentVC resetTransform];
    
    
    NSLog(@"!!!");
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    QDLeftMenuCellType type = indexPath.row;
    QDTabBarController* rightVC = (QDTabBarController*)parentVC.rightVC;
    switch (type) {
        case QDLeftMenuCellTypeDoctorApply:

            [rightVC pushControllerWithStoryName:@"DoctorApply" andIdentifier:@"DoctorApply" andPara:nil];
            break;
        case QDLeftMenuCellTypeSickCaseManger:
            [rightVC pushControllerWithName:@"QDSicknessManagerVC" andPara:nil];
            break;
        case QDLeftMenuCellTypeDoctorAttention:
            [rightVC pushControllerWithStoryName:@"DoctorAttention" andIdentifier:@"DoctorAttention" andPara:nil];
            break;
        case QDLeftMenuCellTypeSetting:
            [rightVC pushControllerWithStoryName:@"Setting" andIdentifier:@"Setting" andPara:nil];
            break;
        case QDLeftMenuCellTypeLogout:
            _isLogin = NO;
            [[QDUserInfo defaultUserInfo] logOut];
            [self loginBack];
            [QDBannerTools pp_BannerSuccessTitle:@"注销成功" andTime:1.0];
            break;
    }
    
    
    
    
}

- (void)loadData{
    
    NSArray* menuArr = @[
                         @{@"name":@"名医申请",
                           },
                         @{@"name":@"病例管理",
                           },
                         @{@"name":@"关注医生",
                           },
                         @{@"name":@"设置与帮助",
                           },
                         @{@"name":@"注销",
                           }
                         ];
    
    menuData = menuArr;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
